import 'dart:convert';
import 'package:flutter_module/common/http/YZHttp.dart';
import 'package:flutter_module/common/third/analytics/YZAnalytics.dart';
import 'package:flutter_module/model/book.dart';
import 'package:flutter_module/page/Book/book_search/book_search_state.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:get/get.dart';
import '../../../common/base/logic/BasicListLogic.dart';
import '../../../common/http/ApiConstant.dart';
import '../../../common/http/YZHttpUtils.dart';
import '../../../common/router/RouterParams.dart';
import '../../../common/router/RouterPath.dart';

/// @description:
/// @author
/// @date: 2022-12-30 15:23:31
class BookSearchLogic extends BasicListLogic<Book> {
  final state = BookSearchState();

  @override
  void onInit() {
    super.onInit();
    requestBookList(true, configNetWorkParmas());
    state.tfController.addListener(() {});
    YZAnalytics.track("D00005", "查找书籍页", "读书笔记", null);
  }

  @override
  void dispose() {
    state.tfController.removeListener(() {});
    super.dispose();
  }

  void pushAddBook() async {
    YZAnalytics.track("D00006", "查找书籍页-添加书籍", "读书笔记", null);
    Book result = await BoostNavigator.instance.push(ROUTER_ADD_BOOK_PAGE,
        arguments: {PARAM_BOOK_NAME: state.tfController.text});
    print("result-> $result");
    if (result.isBlank != true) {
      selectBtnClicked(result);
    }
  }

  void requestBookList(bool isHeaderRefresh, Map<String, dynamic>? info) {
    HttpUtils.get(api_book_list, params: info, successCallBack: (data, total) {
      print('totalrequestBookList->>>>>>> ${total}');
      dealBookListDatas(data, isHeaderRefresh, total);
    }, errorCallBack: (e) {
      failRefresh(isHeaderRefresh, e);
    });
  }

  void dealBookListDatas(dynamic data, bool isHeaderRefresh, int total) {
    List<Book> items = (data as List).map((e) => Book.fromJson(e)).toList();
    if (isHeaderRefresh) {
      dataList = items;
    } else {
      dataList.addAll(items);
    }
    successRefresh(isHeaderRefresh, items.length);
  }

  void selectBtnClicked(Book book) {
    print('${book.bookTitle}');
    state.bookFocusNode.unfocus();
    String bookStr = jsonEncode(book.toJson());
    print('bookStr===$bookStr');
    BoostNavigator.instance.pop({'book': bookStr});
  }

  void clearSearchText() {
    page = 1;
    state.tfController.clear();
    state.searchText = '';
    state.isShowClearBtn = false;
    requestBookList(true, configNetWorkParmas());
  }

  void completeInput(String text) {
    page = 1;
    state.searchText = state.tfController.text;
    state.isShowClearBtn = text.isNotEmpty;
    requestBookList(true, configNetWorkParmas());
  }

  @override
  void footerRetryRefresh() {
    refreshController.requestLoading();
  }

  @override
  Map<String, dynamic>? configNetWorkParmas() {
    return {'page': page, 'pageSize': PAGE_SIZE, 'bookTitle': state.searchText};
  }

  @override
  void getnetworkdata(bool isHeaderRefresh, Map<String, dynamic>? info) {
    requestBookList(isHeaderRefresh, configNetWorkParmas());
  }
}
